package cn.smbms.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smbms.bean.model.Staff;
import cn.smbms.bean.model.SysRole;
import cn.smbms.bean.model.UserRole;
import cn.smbms.bean.vo.UserRoleVo;
import cn.smbms.biz.IUserRoleBiz;

@Controller
@RequestMapping("/userRole")
public class UserRoleController {

	@Resource(name = "userRoleBiz")
	private IUserRoleBiz userRoleBiz;

	List<UserRole> list = null;

	@RequestMapping("/get")
	public String get(HttpServletRequest request, UserRoleVo userRoleVo,
			Model model) { // 获取所有角色信息
		try {
			list = userRoleBiz.getUserRoleByUserRoleVo(userRoleVo);

			model.addAttribute("list", list);
			model.addAttribute("userRoleVo", userRoleVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "userRole/index";
	}

	/**
	 * 获取数据，进入添加页面
	 */
	@RequestMapping("/addPage")
	public String addPage(Model m) {
		List<Staff> staffs = userRoleBiz.StaffList();
		List<SysRole> sysRoles = userRoleBiz.SysRoleList(null);
		m.addAttribute("staffs", staffs);
		m.addAttribute("sysRoles", sysRoles);
		return "userRole/add";
	}

	@RequestMapping(value = "/add")
	public String add(UserRole userRole, Model model)
			throws UnsupportedEncodingException { // 执行添加角色信息操作
		int result = userRoleBiz.add(userRole);
		if (result > 0) {
			// model.addAttribute("msg", "添加成功！");
			return "redirect:/userRole/get/"; // 重定向
		} else {
			// model.addAttribute("msg", "添加失败！");
			return "userRole/addStaff";
		}
	}

	@RequestMapping("/modifyPage")
	public String modifyPage(
			@RequestParam(value = "userRoleId") Integer userRoleId, Model model) { // 进入修改页面

		UserRole userRole = userRoleBiz
				.findUserRoleByUserRoleId(userRoleId);
		List<SysRole> sysRoles = userRoleBiz.SysRoleList(null);
		model.addAttribute("sysRoles", sysRoles);
		model.addAttribute("userRole", userRole);
		return "userRole/modify";
	}

	@RequestMapping(value = "/update", produces = { "application/json;charset=UTF-8" })
	public String update(UserRole userRole, Model model)
			throws UnsupportedEncodingException { // 修改
		System.out.println("qqqqqqqqqqqqqqqqq:" + userRole.getChnName()
				+ " usid: " + userRole.getUserRoleId() + " sf : "
				+ userRole.getRoleId());
		int result = userRoleBiz.update(userRole);
		result = userRoleBiz.update2(userRole);
		System.out.println(result + "fuck!!!!!");
		if (result > 0) {
			// model.addAttribute("msg", "修改成功！");
			return "redirect:/userRole/get/"; // 重定向
		} else {
			// model.addAttribute("msg", "修改失败！");
			return "userRole/modify";
		}
	}

	@RequestMapping(value = "/ajax", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public List<UserRole> ajax(Integer[] myArray, String chnName) {
		/*
		 * String[] sourceStrArray = id.split(",");
		 * System.out.println("sourceStrArray : " + sourceStrArray.length);
		 */

		int cg = 0;
		for (int i = 0; i < myArray.length; i++) {
			cg = userRoleBiz.delete(myArray[i]);
			System.out.println(myArray[i]);
		}
		List<UserRole> projectList = userRoleBiz
				.getUserRoleByUserRoleVo(new UserRoleVo(chnName));
		return projectList;
	}

	/**
	 * 单个删除
	 */
	@RequestMapping(value = "/delOne", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String delOne(Integer id) {
		System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
		int cg = 0;
		cg = userRoleBiz.delete(id);
		return "";
	}

	/**
	 * 无刷新查询，换页，模糊
	 */
	@RequestMapping(value = "/ajaxQuery", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public List<UserRole> ajaxQuery(UserRoleVo userRoleVo) {
		if (userRoleVo.getPageIndex() < 1) {
			userRoleVo.setPageIndex(1);
		}

		List<UserRole> roleList = userRoleBiz
				.getUserRoleByUserRoleVo(userRoleVo);
		System.out.println("chnName:" + userRoleVo.getChnName());
		return roleList;
	}

	/**
	 * 验证添加
	 */
	@RequestMapping(value = "/addAjax", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Integer addAjax(Integer staffId, Integer roleId) {
		System.out.println("staffId : " + staffId + " roleId : " + roleId);
		if (staffId == null) {
			return 1;
		}
		if (roleId == null) {
			return 2;
		}
		Staff staff = userRoleBiz.fetchStaff(staffId);
		if (staff == null) {
			return 1;
		}

		SysRole sysRole = userRoleBiz.fetchSysRole(roleId);
		if (sysRole == null) {
			return 2;
		}
		return 0;
	}

	/**
	 * 验证修改
	 */
	@RequestMapping(value = "/updAjax", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Integer updAjax(Integer roleId) {
		if (roleId == null) {
			System.out.println("11111111111111111111111");
			return 2;
		}
		SysRole sysRole = userRoleBiz.fetchSysRole(roleId);
		if (sysRole == null) {
			System.out.println("22222222222222222222");
			return 2;
		}
		System.out.println("33333333333333333333333333333333333");
		return 0;
	}

	/**
	 * 如果没有用户可以添加，就不让跳转
	 */
	@RequestMapping(value = "/addAjax2", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Integer addAjax2() {
		List<Staff> staffs = userRoleBiz.StaffList();
		if (staffs.size() == 0) {
			return 1;
		}
		return 0;
	}

	@RequestMapping(value = "/ajaxCount", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public Integer ajaxCount(String chnName) {
		UserRoleVo urv = userRoleBiz.count(chnName);
		System.out.println("3 xxxxxxxxxxxxxxxxxxxxx:" + chnName + " 数量："
				+ urv.getCount());
		return urv.getCount();
	}

	public IUserRoleBiz getUserRoleService() {
		return userRoleBiz;
	}

	public void setUserRoleService(IUserRoleBiz userRoleService) {
		this.userRoleBiz = userRoleService;
	}

}